package com.hp.windows;

import com.hp.windows.listener.MyDOMEventListener;
import com.hp.windows.listener.MyLoadListener;
import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.dom.By;

import com.teamdev.jxbrowser.chromium.dom.DOMDocument;
import com.teamdev.jxbrowser.chromium.dom.DOMElement;
import com.teamdev.jxbrowser.chromium.dom.events.DOMEventType;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

import javax.swing.*;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.List;

/**
 * 窗口
 */
//osi应用架构 7
//应用层 hhtp ftp jdbc file
//表现层 java controller
//会话层 web session
//网络层 sokcet
//传输层
//数据库链路层
//物理层
public class MyFrame extends JFrame {
    //shift+ctrl+u  调大小写
    //获取窗口的大小
    public static final Dimension SIZE = Toolkit.getDefaultToolkit().getScreenSize();
    //屏宽
    public static final  int WINDOWS_WIDTH=SIZE.width;
    //屏高
    public static final  int WINDOWS_HEIGH=SIZE.width;
    //上部分
    Browser up_browser=new Browser();
    //下部分
    Browser down_browser=new Browser();

    //1080*1920
    public MyFrame(){

        BrowserView up_view = new BrowserView(up_browser);//创建up视图
        BrowserView down_view = new BrowserView(down_browser);//创建down视图

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(WINDOWS_WIDTH,WINDOWS_HEIGH);
        this.setUndecorated(true);

        //绝对布局
        JPanel panel=new JPanel(null);

        up_view.setBounds(0,0,WINDOWS_WIDTH,400);
        down_view.setBounds(0,400,WINDOWS_WIDTH,WINDOWS_HEIGH-400);

        panel.add(up_view);
        panel.add(down_view);

        this.setContentPane(panel);
        String url= MyFrame.class.getClassLoader().getResource("view/index.html").toString();
        up_browser.loadURL(url);
        down_browser.loadURL("http://www.kepuchina.cn/");

        setHrefListener();
        setRemoveTarget();//删除下部分的target
        this.setVisible(true);
    }

    private void setRemoveTarget() {
        //删除第一次加载的页面target
        DOMDocument document = down_browser.getDocument();
        List<DOMElement> elements = document.findElements(By.tagName("a"));
        for (DOMElement element : elements) {
            element.removeAttribute("target");
        }
        down_browser.addLoadListener(new MyLoadListener(down_browser));
    }

    private void setHrefListener() {

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        DOMDocument document = up_browser.getDocument();//得到整个页面的document
        List<DOMElement> list = document.findElements(By.tagName("a"));//获取所有的a标签
        List<String> hrefs = new ArrayList<String>();//用来装标签的集合
        for (DOMElement domElement : list) {
            String uri = domElement.getAttribute("href");//获取跳转的uri
            domElement.removeAttribute("href");//去掉页面的a标签防止页面跳转
            hrefs.add(uri);
        }

        List<DOMElement> lis = document.findElements(By.tagName("li"));//获取当前页面所有li标签
        for (int i = 0; i < lis.size(); i++) {
            DOMElement domElement = lis.get(i);
            String url = hrefs.get(i);
            domElement.addEventListener(DOMEventType.OnMouseDown, new MyDOMEventListener(up_browser, down_browser, url), true);
        }

    }

    public static void main(String[] args){
        //target
        new MyFrame();
    }
}
